// Created by liusc on 2020/8/21-15:24.

import { getItem, getItem2, getObj, saveTemplateData, updateTemplateData, saveOrUpdateItem } from "@/views/wy-budget/api/budgettemplate.js";
import { getItemObj, delObj } from "@/views/wy-budget/api/budgettemplateitem.js";


export default {
    name: "template_edit",

    data () {
        var doubleValidate = (rule, value, callback) => {
            let reg = /^(?:[1-9]?\d|100)$/;
            let lable = rule.message;
            if (!reg.test(value)) {
                return callback(new Error("数值格式错误"));
            } else {
                callback();
            }
        };
        return {
            parentId: '',
            addAHidden: false,
            itemValueList: [],
            importability: true,
            form: {
                budgetTemplate: {
                    templateId: '',
                    templateName: '',
                    remarks: '',
                },
                budgetTemplateItem: {
                    // itemId: '',
                    parentId: '',
                    templateId: '',
                    templateName: '',
                    itemName: '',
                    itemWarn: '',
                    isNead: '0',
                    remarks: '',
                },
            },
            rules: {
                budgetTemplateItem: {
                    itemWarn: [
                        { required: true, message: '请输入预警值 ', trigger: 'blur' },
                        { validator: doubleValidate, trigger: "blur" },
                    ],
                }
            }
            ,
        };
    },

    computed: {
    },

    mounted () {
        this.getObj()
    },

    created () {

    },

    methods: {
        //获取列表
        getObj () {
            const loading = this.$loading({
                lock: true,
                text: "数据加载中"
            });
            getObj(this.$route.params.templateId)
                .then(respData => {
                    this.$refs["add-form"].resetFields();
                    this.form.budgetTemplate = respData.data.data;
                    this.initItem(this.form.budgetTemplate.templateId);
                    loading.close();

                })
                .catch(e => {
                    loading.close();
                    this.$message({
                        type: "error",
                        message: "数据获取失败!"
                    });
                });
        },
        //删除科目
        delObj (v) {
            delObj(v).then(res => {
                this.getObj()

                this.$message({
                    message: "删除科目成功",
                    type: 'success'
                });
            }).catch((err) => {

            });
        },
        // 新增科目清楚表单
        addSubjects () {

            if (this.importability) {
                this.form.budgetTemplateItem = {
                    parentId: '',
                    templateId: '',
                    templateName: '',
                    itemName: '',
                    itemWarn: '',
                    isNead: '0',
                    remarks: '',
                }
                this.addAHidden = false
                this.addAHidden = true
                this.form.budgetTemplateItem
                    .templateId = this.form.budgetTemplate.templateId
                this.form.budgetTemplateItem
                    .templateName = this.form.budgetTemplate.templateName
                this.form.budgetTemplateItem
                    .itemWarn = ''
                this.form.budgetTemplateItem
                    .itemName = ''
                this.form.budgetTemplateItem
                    .remarks = ''
                this.form.budgetTemplateItem
                    .remarks = ''
                this.form.budgetTemplateItem
                    .isNead = '0'
                this.parentId == '' ? this.form.budgetTemplateItem.parentId = '' : this.form.budgetTemplateItem
                    .parentId = this.parentId
            }
        },
        // 树形结构变化
        handleCheckChange (data, checked) {
            this.parentId = ''
            getItemObj(data.itemId).then(res => {
                this.form.budgetTemplateItem = res.data.data
                this.addAHidden = true
                if (data.parentId == '' || data.parentId == null) {
                    this.importability = true
                    this.parentId = data.itemId

                } else {
                    this.importability = false
                }
            }).catch((err) => {
            });
        },
        // 获取科目
        initItem () {
            getItem2(this.form.budgetTemplate.templateId).then(rep => {
                let rspD = rep.data.data;
                if (rspD) {
                    rspD = rspD.map((rs, index) => {
                        //rs["sortNum"] = index + 1;    
                        return rs;
                    });
                }
                this.itemValueList = rspD;
            });
        },

        // 取消
        cancel () {
            this.closeNowRouter();
        },

        // 保存
        addCommit () {
            this.$refs["add-form"].validate(valid => {
                if (valid) {
                    this.sendFormData();
                }
            });
        },

        sendFormData (val) {
            saveOrUpdateItem(this.form.budgetTemplateItem)
                .then(rep => {
                    this.success_message("保存成功");
                    this.closeNowRouter();
                })
                .catch(e => {
                    this.errormessage("保存失败");
                });
        },

        success_message (msg) {
            this.$message({
                type: "success",
                message: msg
            });
        },

        errormessage (msg) {
            this.$message({
                type: "error",
                message: msg
            });
        },

        closeNowRouter () {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            //跳转
            this.$router.push({ name: "template_list" });
        },
    },
};
